package com.hui;

import com.hui.data.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.mybatis.spring.batch.builder.MyBatisBatchItemWriterBuilder;
import org.springframework.batch.core.*;
import org.springframework.batch.core.annotation.AfterChunk;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.core.launch.JobParametersNotFoundException;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.support.SimpleJobOperator;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.CompositeItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Configuration
@Slf4j
public class SpringbatchLearn16Configuration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    @Bean
    public Job mybatisJob() {
        return jobBuilderFactory.get("mybatisJob").start(mybatisStep()).build();
    }

    @Bean
    public Step mybatisStep() {
        return stepBuilderFactory.get("mybatisStep")
                .<User, User>chunk(2)
                .reader(mybatisReader())
                .processor(mybatisProcess())
                .writer(mybatisWrite())
                .build();
    }

    @Bean
    public ItemWriter<User> mybatisWrite() {

        CompositeItemWriter compositeItemWriter = new CompositeItemWriter();

        compositeItemWriter.setDelegates(new ArrayList<ItemWriter>(){{


            add((ItemWriter<User>) list -> {
                for (User user : list) {
                    System.out.println(user.toString());
                }
            });


            add(new MyBatisBatchItemWriterBuilder<User>()
                    .sqlSessionFactory(sqlSessionFactory)
                    .statementId("com.hui.mapper.UserMapper.addAll").build());
        }});

        return compositeItemWriter;
    }

    @Bean
    public ItemProcessor<User, User> mybatisProcess() {
        return new ItemProcessor<User, User>() {
            @Override
            public User process(User user) throws Exception {
                return user;
            }
        };
    }

    @Bean
    public ItemReader<User> mybatisReader() {
        return new MyBatisPagingItemReader<User>(){{
            setPageSize(2);
            setSqlSessionFactory(sqlSessionFactory);
            setQueryId("com.hui.mapper.UserMapper.findAll");
        }};
    }

}
